#pragma once
#include "InetAddr.hpp"
#include "Common.hpp"
//#include "ThreadPool.hpp"
#include <sys/types.h>
#include <memory>
#include <sys/wait.h>

namespace kzz
{
    const int gbacklog = 128;
    class Socket
    {
    public:
       // virtual ~Socket() = 0;
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        virtual  std::shared_ptr<Socket> Accept(InetAddr& client_addr) = 0;
        virtual int Recv(std::string &out) = 0;
         virtual int Fd() = 0;

        virtual int Send(std::string &massage) = 0;
        virtual int Connect(const std::string &server_ip, uint16_t server_port) = 0;
        virtual void Close() = 0;
        void BuildTcpServerSocketMethod(uint16_t port, int backlog = gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }

        void BuildTcpClientSocketMethod()
        {
            SocketOrDie();
        }
    };
    class TcpSocket : public Socket
    {
    public:
        TcpSocket()
            : _sockfd(-1)
        {
        }
        TcpSocket(int fd)
            : _sockfd(fd)
        {
        }
         int Fd()override
         {
            return _sockfd;
         }
        void SocketOrDie() override
        {
            _sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                kzz::LOG(LogLevel::ERROR) << "创建套接字失败";
                exit(SOCKET_ERR);
            }
            kzz::LOG(LogLevel::ERROR) << "socket success,sockfd:" << _sockfd;
        }

        void BindOrDie(uint16_t port) override
        {
            kzz::InetAddr local(port);
            int n = bind(_sockfd, local.Addr(), local.Len());

            if (n != 0)
            {
                kzz::LOG(LogLevel::ERROR) << "bind error";
                exit(BIND_ERR);
            }
            kzz::LOG(LogLevel::ERROR) << "bind success";
        }
        void ListenOrDie(int backlog = gbacklog) override
        {
            int n = listen(_sockfd, backlog);
            if (n < 0)
            {
                kzz::LOG(LogLevel::ERROR) << "listen error";
                exit(LISTEN_ERR);
            }
            kzz::LOG(LogLevel::ERROR) << "listen success";
        }
        // 输出型参数
        // 返回值是整个accept接受的文件描述符创建的socket，
        std::shared_ptr<Socket> Accept(InetAddr& client_addr) override
        {
            char buffer[1024*4];
            sockaddr_in client;
            socklen_t len = sizeof(client);
            int client_fd = accept(_sockfd, (sockaddr *)&client, &len);
            // kzz::InetAddr client_addr(client);
            client_addr.SetAddr(client);
        
            if (client_fd < 0)
            {
                // 接受失败
                kzz::LOG(LogLevel::ERROR) << "accept error";
                exit(ACCEPT_ERR);
            }
            return std::make_shared<TcpSocket>(client_fd);
        }

        void Close() override
        {
            int n = close(_sockfd);
            if (n != 0)
            {
                kzz::LOG(LogLevel::ERROR) << "close error";
                exit(CLOSE_ERR);
            }
        }
        // 只关心读到什么
        int Recv(std::string &out) override
        {
            ////////////////////////////////////////
            //栈帧复用导致的问题
            //char buffer[1024];
            ////////////////////////////////////////////
            char buffer[4096*2]={0};
         // std::cout<<"buffer的地址："<<&buffer<<std::endl;
            int n = recv(_sockfd, buffer, sizeof(buffer), 0);
            if (n > 0)
            {
                out += buffer;
            }
            if (n < 0)
            {
                kzz::LOG(LogLevel::ERROR) << "recv error";
                exit(RECV_ERR);
            }
            return n;
        }
        // 发消息
        int Send(std::string &massage) override
        {
            return send(_sockfd, massage.c_str(), massage.size(), 0);
        }
        // 链接
        int Connect(const std::string &server_ip, uint16_t server_port) override
        {
            InetAddr server_addr(server_ip.c_str(), server_port);
            int n = connect(_sockfd, server_addr.Addr(), server_addr.Len());
            if (n < 0)
            {
                kzz::LOG(LogLevel::ERROR) << "connect error";
                exit(CONNECT_ERR);
            }
            return n;
        }

        // ~TcpSocket() override
        // {
        // }

    private:
        int _sockfd; //
    };

}